package common.web.action;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import util.StringUtil;

import common.service.BaseService;
import common.service.CacheService;
import common.service.IService;
import common.service.ServiceFactory;
import common.splitpage.PageInfo;
import common.web.WebHelper;

public class BaseAction {
	private static Logger logger = Logger.getLogger(BaseAction.class.getName());
			
	//获取所有数据,用于不分页的列表页面
	public static List<Map<String,Object>> getListData(HttpServletRequest request) throws Exception {
		//获取service的代理类
		WebHelper webHelper = new WebHelper();
		webHelper.setRequest(request);
		IService service = ServiceFactory.getService(null,webHelper);
				
		return service.getListData();
	}
	
	//获取分页数据,用于分页列表页面
	public static PageInfo getPageInfo(HttpServletRequest request) throws Exception {
		//获取service的代理类
		WebHelper webHelper = new WebHelper();
		webHelper.setRequest(request);
		IService service = ServiceFactory.getService(null,webHelper);

		return service.getPageInfo();
	}
	
	//根据ID获取数据,用于编辑页面
	public static Map<String,Object> getEntityData(HttpServletRequest request) throws Exception {
		//获取service的代理类
		WebHelper webHelper = new WebHelper();
		webHelper.setRequest(request);
		IService service = ServiceFactory.getService(null,webHelper);

		return service.getEntityData();
	}
	
	//保存
	public static void doSave(HttpServletRequest request) throws Exception {
		//获取service的代理类
		WebHelper webHelper = new WebHelper();
		webHelper.setRequest(request);
		IService service = ServiceFactory.getService(null,webHelper);
		
		service.save();
	}
	
	//删除
	public static void doDel(HttpServletRequest request) throws Exception {
		String tableName = request.getParameter("tableName");
		
		String pkName = "";
		List<Map<String,Object>> configList = CacheService.getDataByCacheId("SYSMAN_TABLE_CONFIG", "TABLE_NAME", tableName);
		for(Map<String,Object> configMap : configList) {
			String fieldName = (String)configMap.get("FIELD_NAME");//字段名
			Integer isPk = (Integer)configMap.get("IS_PK");//是否主键
			if(1 == isPk) {
				pkName= fieldName;
				break;
			}
		}
		String[] pkArr = request.getParameterValues(pkName);
		//String sql = "delete from " + tableName + " where " + pkName + " in (" + StringUtil.getMoreIdCond(pkArr) + ")";
		String sql = "update " + StringUtil.getTableName(tableName) + " set ENABLED=0 where " + pkName + " in (" + StringUtil.getMoreIdCond(pkArr) + ")";

		//IService service = ServiceFactory.getService(null);
		IService service = new BaseService();
		service.executeBySql(sql);
	}
	
	
	
}
